﻿using UnityEngine;
using System.Collections;
using UI;
public class 二级弹窗 : BasePanel 
{
    public Object _object;
   private RectTransform thisRect;
    private RectTransform canvas;
    private float thisWidth;
    // Use this for initialization
    void Awake()
    {
        thisRect = gameObject.GetComponent<RectTransform>();
        canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
        thisWidth = thisRect.sizeDelta.x;
        print(thisRect.sizeDelta);
    }

    public override void OnEnter(BasePanel panel)
    {

        gameObject.SetActive(true);
        RectTransform rect = panel.gameObject.GetComponent<RectTransform>();
        float width = rect.sizeDelta.x / 2;
        float height = rect.sizeDelta.y / 2;
        float sumWidth = 0;
        float sumheight = rect.anchoredPosition.y + height;
        if (rect.anchoredPosition.x < 0)
        {
            sumWidth = rect.anchoredPosition.x - width;
            try
            {
                if (Mathf.Abs(sumWidth - thisRect.sizeDelta.x) > canvas.sizeDelta.x / 2)
                {
                    sumWidth = rect.anchoredPosition.x + thisRect.sizeDelta.x + width;
                }
            }
            catch (System.Exception e)
            {
                print(e);
            }

        }
        if (rect.anchoredPosition.x>0)
        {
            sumWidth = rect.anchoredPosition.x - width;
            try
            {
                if (Mathf.Abs(sumWidth + thisRect.sizeDelta.x) > canvas.sizeDelta.x / 2)
                {
                    sumWidth = rect.anchoredPosition.x  - width;
                }
            }
            catch (System.Exception)
            {

            }
        }
        if (rect.anchoredPosition.y < 0)
        {
            try
            {
                if (Mathf.Abs(sumheight - thisRect.sizeDelta.y) > canvas.sizeDelta.y / 2)
                {
                    sumheight += ((Mathf.Abs(sumheight - thisRect.sizeDelta.y)) - (canvas.sizeDelta.y / 2));
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }
        transform.localPosition = new Vector3(sumWidth, sumheight, 0);

        //float 
        //transform.position = Input.mousePosition;
    }
    public override void OnExit()
    {
        gameObject.SetActive(false);
    }
    public void 关闭界面()
    {
        UIManager.Instance.PopPanel();
    }
    public void 添加属性(Transform cont)
    {
        GameObject obj = Instantiate(_object) as GameObject;
        obj.transform.parent = cont;
    }
    // Update is called once per frame
    void Update()
    {

    }
}
